﻿/***********************************************************
Copyright © 2015 Howyoung.
************************************************************
You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty.
************************************************************
您可以对所收受的本程序源代码，无论以何种媒介，复制与发布其完
整的复制物，然而您必须以显著及适当的方式在每一份复制物上发布
适当的著作权标示及无担保声明。
***********************************************************/
using System;
using System.IO;
using System.Windows.Forms;

namespace MinecraftSavesBackup
{
    public partial class Welcome : Form
    {
        int pictureCount = 1;

        public Welcome()
        {
            InitializeComponent();
        }
        //HSXML支持函数
        private string HSXMLRead(string path, string name)
        {
            string before = "<" + name + ">", after = "<" + name + "/>", all = File.ReadAllText(path), foundText = null;
            int beforeCount = 0, afterCount = 0;
            bool leftFound = false, rightFound = false;
            for (beforeCount = before.Length; beforeCount <= all.Length; beforeCount++)
            {
                string str1 = all.Substring(0, beforeCount);
                string str2 = str1.Substring(str1.Length - before.Length, before.Length);
                if (str2 == before)
                {
                    leftFound = true;
                    break;
                }
            }
            if (!leftFound)
            {
                return null;
            }
            else
            {
                all = all.Substring(all.Length - (all.Length - beforeCount), all.Length - beforeCount);
                for (afterCount = after.Length; afterCount <= all.Length; afterCount++)
                {
                    string s1 = all.Substring(0, afterCount);
                    string s2 = s1.Substring(s1.Length - after.Length, after.Length);
                    if (s2 == after)
                    {
                        rightFound = true;
                        break;
                    }
                }
                if (!rightFound)
                {
                    return null;
                }
                else
                {
                    foundText = all.Substring(0, afterCount - after.Length);
                    return foundText;
                }
            }
        }

        private int HSXMLCount(string Path)
        {
            TextBox textBox = new TextBox();
            textBox.Text = File.ReadAllText(Path);
            textBox.Multiline = true;
            int getcount = 0;
            foreach (string text in textBox.Lines)
            {
                if (text.Contains("<") && text.Contains("/>") && text.Contains(">")) getcount++;
            }
            return getcount;
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            switch (pictureCount)
            {
                case 1:
                    PictureBox1.Image = Properties.Resources.w1;
                    break;
                case 2:
                    PictureBox1.Image = Properties.Resources.w2;
                    break;
                case 3:
                    PictureBox1.Image = Properties.Resources.w3;
                    break;
                case 4:
                    PictureBox1.Image = Properties.Resources.w4;
                    break;
                case 5:
                    PictureBox1.Image = Properties.Resources.w5;
                    break;
                case 6:
                    PictureBox1.Image = Properties.Resources.w6;
                    break;
                case 7:
                    PictureBox1.Image = Properties.Resources.w7;
                    break;
                case 8:
                    PictureBox1.Image = Properties.Resources.w8;
                    break;
                case 9:
                    PictureBox1.Image = Properties.Resources.w9;
                    break;
                case 10:
                    PictureBox1.Image = Properties.Resources.w10;
                    break;
                case 11:
                    PictureBox1.Image = Properties.Resources.w11;
                    break;
                case 12:
                    PictureBox1.Image = Properties.Resources.w12;
                    break;
                case 13:
                    PictureBox1.Image = Properties.Resources.w13;
                    break;
                case 14:
                    PictureBox1.Image = Properties.Resources.w14;
                    break;
                case 15:
                    PictureBox1.Image = Properties.Resources.w15;
                    break;
                case 16:
                    PictureBox1.Image = Properties.Resources.w16;
                    break;
                case 17:
                    PictureBox1.Image = Properties.Resources.w15;
                    break;
                case 18:
                    PictureBox1.Image = Properties.Resources.w14;
                    break;
                case 19:
                    PictureBox1.Image = Properties.Resources.w13;
                    break;
                case 20:
                    PictureBox1.Image = Properties.Resources.w12;
                    break;
                case 21:
                    PictureBox1.Image = Properties.Resources.w11;
                    break;
                case 22:
                    PictureBox1.Image = Properties.Resources.w10;
                    break;
                case 23:
                    PictureBox1.Image = Properties.Resources.w9;
                    break;
                case 24:
                    PictureBox1.Image = Properties.Resources.w8;
                    break;
                case 25:
                    PictureBox1.Image = Properties.Resources.w7;
                    break;
                case 26:
                    PictureBox1.Image = Properties.Resources.w6;
                    break;
                case 27:
                    PictureBox1.Image = Properties.Resources.w5;
                    break;
                case 28:
                    PictureBox1.Image = Properties.Resources.w4;
                    break;
                case 29:
                    PictureBox1.Image = Properties.Resources.w3;
                    break;
                case 30:
                    PictureBox1.Image = Properties.Resources.w2;
                    break;
            }
            pictureCount++;
            if (pictureCount > 30)
            {
                Timer1.Enabled = false;
                PictureBox1.Image = Properties.Resources.w1;
                pictureCount = 1;
            }
        }

        private void PictureBox1_Click(object sender, EventArgs e)
        {
            Timer1.Enabled = true;
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            //检测并导入旧设置
            try
            {
                //从ApplicationData文件夹转移
                string programDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string settingsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                if (settingsDirectory.EndsWith("\\"))
                {
                    settingsDirectory += "MinecraftSavesBackup\\";
                }
                else
                {
                    settingsDirectory += "\\MinecraftSavesBackup\\";
                }
                Directory.CreateDirectory(settingsDirectory);
                string[] settingsFileNames = new string[] { "BackupFolder.hsxml", "BackupPath.ini", "Config.hsxml", "MD5.hsxml", "savesPath.ini" };
                foreach (string settingsFileName in settingsFileNames)
                {

                    if (File.Exists(programDirectory + settingsFileName)) File.Delete(programDirectory + settingsFileName);
                    if (File.Exists(settingsDirectory + settingsFileName)) File.Copy(settingsDirectory + settingsFileName, programDirectory + settingsFileName);
                    if (File.Exists(settingsDirectory + settingsFileName)) File.Delete(settingsDirectory + settingsFileName);
                }
                if (Directory.Exists(settingsDirectory)) Directory.Delete(settingsDirectory);
                //导入
                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "BackupFolder.hsxml") &&
                    File.Exists(AppDomain.CurrentDomain.BaseDirectory + "BackupPath.ini") &&
                    File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml") &&
                    File.Exists(AppDomain.CurrentDomain.BaseDirectory + "MD5.hsxml") &&
                    File.Exists(AppDomain.CurrentDomain.BaseDirectory + "savesPath.ini") &&
                    MessageBox.Show("发现旧版本的配置文件，是否导入设置？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {

                    nextButton.Text = "请稍后";
                    nextButton.Enabled = false;
                    //写入BackupDirectoryAddress
                    Properties.Settings.Default.BackupDirectoryAddress.Clear();
                    for (int i = 1; i <= HSXMLCount(AppDomain.CurrentDomain.BaseDirectory + "BackupFolder.hsxml"); i++)
                    {
                        string backupDirectoryAddress = HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "BackupFolder.hsxml", Convert.ToString(i));
                        Properties.Settings.Default.BackupDirectoryAddress.Add(backupDirectoryAddress);
                    }
                    //记录备份地址
                    string backupAddress = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "BackupPath.ini");
                    if (backupAddress.EndsWith("\\"))
                    {
                        Properties.Settings.Default.BackupAddress = backupAddress;
                    }
                    else
                    {
                        Properties.Settings.Default.BackupAddress = backupAddress + "\\";
                    }
                    Directory.CreateDirectory(Properties.Settings.Default.BackupAddress);
                    //读取配置文件(Config.hsxml)
                    Properties.Settings.Default.AutomaticBackup = (HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "AutomaticBackup") == "True");
                    Properties.Settings.Default.AutomaticBackupByTime = (HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "AutomaticBackupType") == "AtSetIntervals");
                    Properties.Settings.Default.AutomaticBackupTime = Convert.ToInt32(HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "AutomaticBackupTime"));
                    Properties.Settings.Default.AutomaticBackupIfChanged = (HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "AutomaticBackupCondition") == "FindDifference");
                    Properties.Settings.Default.AutomaticallyRemoveBackup = (HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "AutomaticDeleteBackup") == "True");
                    Properties.Settings.Default.AutomaticallyRemoveBackupByFilesNumber = (HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "AutomaticDeleteBackupCondition") == "MaxBackupTotal");
                    Properties.Settings.Default.MaxBackupFilesNumber = Convert.ToInt32(HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "MaxBackupTotal"));
                    Properties.Settings.Default.MaxBackupFileSize = Convert.ToInt32(HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "LargestBackupSize"));
                    Properties.Settings.Default.ShowBackupIco = (HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "ShowBackupAnimation") == "True");
                    Properties.Settings.Default.ZipLevel = Convert.ToInt32(HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "CompressionLevel"));
                    Properties.Settings.Default.GameAddress = HSXMLRead(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml", "GamePath");
                    //写入SaveAddress
                    string savesAddress = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "savesPath.ini");
                    if (savesAddress.EndsWith("\\"))
                    {
                        Properties.Settings.Default.SaveAddress = savesAddress;
                    }
                    else
                    {
                        Properties.Settings.Default.SaveAddress = savesAddress + "\\";
                    }
                    //写入MD5
                    Properties.Settings.Default.MD5.Clear();
                    TextBox textBox = new TextBox();
                    textBox.Text = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "MD5.hsxml");
                    textBox.Multiline = true;
                    foreach (string text in textBox.Lines)
                    {
                        if (text.Contains("<") && text.Contains("/>") && text.Contains(">"))
                        {
                            string before = "<", after = ">", all = text, foundText = null;
                            int beforeCount = 0, afterCount = 0;
                            bool leftFound = false, rightFound = false;
                            for (beforeCount = before.Length; beforeCount <= all.Length; beforeCount++)
                            {
                                string str1 = all.Substring(0, beforeCount);
                                string str2 = str1.Substring(str1.Length - before.Length, before.Length);
                                if (str2 == before)
                                {
                                    leftFound = true;
                                    break;
                                }
                            }
                            if (!leftFound)
                            {
                                throw new Exception("转换MD5.hsxml文件时出错，无法确定文件名。");
                            }
                            else
                            {
                                all = all.Substring(all.Length - (all.Length - beforeCount), all.Length - beforeCount);
                                for (afterCount = after.Length; afterCount <= all.Length; afterCount++)
                                {
                                    string s1 = all.Substring(0, afterCount);
                                    string s2 = s1.Substring(s1.Length - after.Length, after.Length);
                                    if (s2 == after)
                                    {
                                        rightFound = true;
                                        break;
                                    }
                                }
                                if (!rightFound)
                                {
                                    throw new Exception("转换MD5.hsxml文件时出错，无法确定文件名。");
                                }
                                else
                                {
                                    foundText = all.Substring(0, afterCount - after.Length);
                                    string fileMD5 = text.Replace("<" + foundText + "/>", null);
                                    fileMD5 = fileMD5.Replace("<", "{");
                                    fileMD5 = fileMD5.Replace(">", "}");
                                    Properties.Settings.Default.MD5.Add(fileMD5);
                                }
                            }
                        }
                    }
                    Properties.Settings.Default.Initialization = false;
                    Properties.Settings.Default.Save();
                    //删除旧配置文件
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + "BackupFolder.hsxml");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + "BackupPath.ini");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + "Config.hsxml");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + "MD5.hsxml");
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + "savesPath.ini");
                    nextButton.Text = "下一步(&N)";
                    nextButton.Enabled = true;
                    Main Main = new Main();
                    Main.Show();
                    Dispose();
                }
                else
                {
                    EnterSavesAddress EnterSavesAddress = new EnterSavesAddress();
                    EnterSavesAddress.Show();
                    Dispose();
                }
            }
            catch (Exception ex)
            {
                HandlingErrors.ErrorMessage(ex, "再试一次或重新完成初始化配置。");
                HandlingErrors.Record(ex);
                nextButton.Text = "下一步(&N)";
                nextButton.Enabled = true;
            }
        }

        private void Welcome_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.ExitThread();
        }
    }
}


   
